package com.gisuni.sddc.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import com.gisuni.sddc.model.BaseUser;
import com.gisuni.sddc.model.Userinfo;
import com.gisuni.sddc.platformManager.model.Organization;
import com.gisuni.sddc.platformManager.model.UserOrganization;
import com.gisuni.sddc.util.StringUtil;

@IocBean
public class UserInfoDao {
	@Inject
	private Dao dao;  
	
 
	public BaseUser userupdata(BaseUser baseUser){
		dao.update(baseUser);
		return baseUser;
	}
 
    public Boolean updateUserOrganization(String updateSql ){
    	
    	Boolean result = false;
    	Sql sql = Sqls.create(updateSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				return true;
			}
		});
	    dao.execute(sql);
	    return  sql.getObject(Boolean.class );
 
    }
 
 
 
 
	public String executeSQL(String  selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				JSONArray result=new JSONArray();
				int numberOfColumns = rs.getMetaData().getColumnCount();
				 
			     while (rs.next()){
			    	 JSONObject ret=new JSONObject();
						// 遍历查询结果字段
						for (int i = 0; i < numberOfColumns; i++) {
							Object obj = rs.getString(i + 1);
							ret.put(rs.getMetaData().getColumnName(i+1).trim(), obj);
						}
			    	 result.put(ret);
			     }
				return result.toString();
			}
		});
	    dao.execute(sql);
	    return  sql.getObject(String.class);
	}
	
	public String getControlRegion(String roleName){
		String result = "";
		if(!StringUtil.isNullOrEmpty(roleName)){
			 
			
		}
		return result;
	}
	
	/**
	 * 查询用户
	 * @param selectSql
	 * @return
	 */
	public String queryUserId(String userName,String password) {
		
		Sql sql = Sqls.create(" select user_id from sddcgisplatform.user_info where user_name ='"+userName+"' and user_password='"+password+"'");
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				String userId = null;
			     while (rs.next()){
			    	 userId = rs.getString("USER_ID");
			     }
				return userId;
			}
		});
	    dao.execute(sql);
	    return  sql.getObject(String.class);
	}
	
	/**
	 * 查询用户直属的部门
	 * @param selectSql
	 * @return
	 */
	public List<UserOrganization> queryUserManagerOrg(String querySql) {
		
		Sql sql = Sqls.create(querySql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				List<UserOrganization> list = new ArrayList<UserOrganization>();
			     while (rs.next()){
			    	UserOrganization org = new UserOrganization();
			    	if (rs.findColumn("USER_ID") > 0 ){
			    		org.setUserId(rs.getString("USER_ID"));
		    	 	}
		    	 	if (rs.findColumn("ORGANIZATION_COMPANY_ID") > 0 ){
		    	 		org.setOrganizationCompanyId(rs.getString("ORGANIZATION_COMPANY_ID"));
		    	 	}
		    	 	if (rs.findColumn("ORGANIZATION_BUSINESS_ID") > 0 ){
		    	 		org.setOrganizationBusinessId(rs.getString("ORGANIZATION_BUSINESS_ID"));
		    	 	}
		    	 	if (rs.findColumn("ORGANIZATION_DEVELOP_ID") > 0 ){
		    	 		org.setOrganizationDevelopId(rs.getString("ORGANIZATION_DEVELOP_ID"));
		    	 	}
		    	 	if (rs.findColumn("USER_NAME") > 0 ){
		    	 		org.setUserName(rs.getString("USER_NAME"));
		    	 	}
		    	 	if (rs.findColumn("MANAGERORGID") > 0 ){
		    	 		org.setManagerOrganizationId(rs.getString("MANAGERORGID"));
		    	 	}
		    	 	if (rs.findColumn("MANAGERORGTYPE") > 0 ){
		    	 		org.setManagerOrganizationType(rs.getString("MANAGERORGTYPE"));
		    	 	}
		    	 	list.add(org);
			     }
				return list;
			}
		});
	    dao.execute(sql);
	    return  sql.getObject(List.class);
	}
	
	/**
	 * 查询所有可管理的部门
	 * @param selectSql
	 * @return
	 */
	public List<Organization> queryAllMenagerOrg(String querySql) {
		
		Sql sql = Sqls.create(querySql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				List<Organization> list = new ArrayList<Organization>();
			     while (rs.next()){
			    	 Organization org = new Organization();
			    	if (rs.findColumn("ID") > 0 ){
			    		org.setId(rs.getString("ID"));
		    	 	}
		    	 	if (rs.findColumn("ORGANIZATION_TYPE") > 0 ){
		    	 		org.setOrganizationType(rs.getString("ORGANIZATION_TYPE"));
		    	 	}
		    	 	list.add(org);
			     }
				return list;
			}
		});
	    dao.execute(sql);
	    return  sql.getObject(List.class);
	}
 
	
}
